/*模拟PHP的GET , 使用时, 可以直接 $_GET['get参数'], 就直接获得GET参数的值*/
var $_GET = (function () {
    var url = window.document.location.href.toString();
    var u = url.split("?");
    if (typeof (u[1]) == "string") {
        u = u[1].split("&");
        var get = {};
        for (var i in u) {
            var j = u[i].split("=");
            get[j[0]] = j[1];
        }
        return get;
    } else {
        return {};
    }
})();

$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/volume/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '卷名称', name: 'volumeName', index: 'volume_name', width: 80 }, 			
			{ label: '创建时间', name: 'createDate', index: 'create_date', width: 80 }, 			
			{ label: '图书id', name: 'bookId', index: 'book_id', width: 80 }, 			
			{ label: '更新时间', name: 'updateDate', index: 'update_date', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        inputs: [{
            volumeName: null,
            checkbox: false
        },
            {
                volumeName: null,
                checkbox: false

            },
            {
                volumeName: null,
                checkbox: false

            },
            {
                volumeName: null,
                checkbox: false

            },
            {
                volumeName: null,
                checkbox: false

            },
            {
                volumeName: null,
                checkbox: false

            },
            {
                volumeName: null,
                checkbox: false

            },
            {
                volumeName: null,
                checkbox: false

            }, {
                volumeName: null,
                checkbox: false

            }, {
                volumeName: null,
                checkbox: false

            }, {
                volumeName: null,
                checkbox: false

            },
        ],
        // 缓存要修改的卷信息
        updateVolumeInfo: {

        },
        // 显示添加章节的dom节点
        showDom: 'list',
        bookinfo: {
            bookname: '加载中'
        },
        q: {
            bookname: null
        },
        showList: true,
        title: null,
        // roleList:{},
        banner: {
            bannerpic: null
        },
        sels: [

        ]
	},
    created: function () {
        // 获取id
        var bookID = $_GET['id'];
        if (!bookID) {
            alert('系统错误,没有获取到书籍ID,请返回重试.');
            return false;
        }

        console.log(bookID, 'bokid');

        $.get(baseURL + "sys/book/findById/" + bookID, function (r) {
            vm.bookinfo = r.book;
        });

        // 加载章节列表

        $(function () {
            $("#jqGrid").jqGrid({
                mtype: 'POST',
                url: baseURL + 'sys/volume/list',
                datatype: "json",
                colModel: [{
                    label: 'ID',
                    name: 'id',
                    index: "id",
                    width: 45,
                    key: true
                },
                    {
                        label: '卷名称',
                        name: 'volumeName',
                        width: 75
                    },
                    {
                        label: '创建时间',
                        name: 'createDate',
                        width: 90
                    },
                    {
                        label: '更新时间',
                        name: 'updateDate',
                        width: 90
                    },
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList: [10, 30, 50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth: true,
                multiselect: true,
                pager: "#jqGridPager",
                jsonReader: {
                    root: "page.records",
                    page: "page.current",
                    total: "page.pages",
                    records: "page.total"
                },
                prmNames: {
                    page: "page",
                    rows: "limit",
                    order: "order",
                },
                postData: {
                    bookUUID: bookID
                },
                gridComplete: function () {
                    //隐藏grid底部滚动条
                    $("#jqGrid").closest(".ui-jqgrid-bdiv").css({
                        "overflow-x": "hidden"
                    });
                }
            });
        });
    },
	methods: {
		query: function () {
			vm.reload();
		},

		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.volume = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.volume.id == null ? "sys/volume/save" : "sys/volume/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.volume),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/volume/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/volume/info/"+id, function(r){
                vm.volume = r.volume;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});